var prefix_path="http://123.56.219.116:8080/virtualcat/manage/cat/";

$(function () {
    $("#uploadButton").click(function () {
        var formData = new FormData($('#uploadForm')[0]);
        $.ajax({
            type: 'post',
            url: prefix_path+"uploadFiles",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
        }).success(function (data) {
            if(data.data.msg=="login"){
                alert("请先登录");
                location.href="login.html";
                return;
            }
            console.info(data);
            alert("上传成功");
            $("span[name='cimg']").html("cat/"+data[0].toString());
            $("span[name='gimg']").html("good/"+data[1].toString());
        }).error(function () {
            alert("上传失败");
        });
    });

    $("#addButton").click(function () {
        $.ajax({
            url: prefix_path+"addCat",
            type:'POST',
            contentType:'application/json',
            dataType:"json",
            data: JSON.stringify({
                "cname":$("input[name='cname']").val(),
                "cimg":$("span[name='cimg']").html(),
                "gname":$("input[name='gname']").val(),
                "gimg":$("span[name='gimg']").html(),
                "nprice":$("input[name='nprice']").val(),
                "oprice":$("input[name='oprice']").val(),
                "cdesc":$("textarea[name='cdesc']").val(),
                "gdesc":$("textarea[name='gdesc']").val(),
                "detail":$("input[name='detail']").val()
            })
        }).success(function (data) {
            if(data.data.msg=="login"){
                alert("请先登录");
                location.href="login.html";
                return;
            }
            console.info(data);
            alert("添加成功");
        }).error(function () {
            alert("添加失败");
        });
    })
});
